@isTest
private class CheckOppTimeoutTest {
    public static String CRON_EXP = '0 0 0 15 3 ? 2022';

    static testMethod void testMethod1() {

        Province__c pro = TestUtils.createProvince();
        insert pro;

        City__c c = TestUtils.createCity();
        c.Province__c = pro.Id;
        insert c;

        District__c d = TestUtils.createDistrict();
        d.City__c = c.Id;
        insert d;
        
        // 客户
        Account acc = TestUtils.createAccount();
        acc.Province__c = pro.Id;
        acc.City__c = c.Id;
        acc.District__c = d.Id;        
        insert  acc;
        // 业务机会
        Opportunity opp1 = TestUtils.createOpportunity();
        opp1.AccountId = acc.Id;
        opp1.StageCanChange__c = '意向';
        opp1.StageName = '意向';
        Opportunity opp2 = TestUtils.createOpportunity();
        opp2.AccountId = acc.Id;
        opp2.StageCanChange__c = '意向';
        opp2.StageName = '意向';
        insert new Opportunity[] {opp1, opp2};

        // 意向产品
        IntentProduct__c ip1 = TestUtils.createIntentProduct();
        ip1.Opportunity__c = opp1.Id;
        IntentProduct__c ip2 = TestUtils.createIntentProduct();
        ip2.Opportunity__c = opp2.Id;
        IntentProduct__c ip3 = TestUtils.createIntentProduct();
        ip3.Opportunity__c = opp2.Id;
        // ip3.Category1__c = '生化';
        insert new IntentProduct__c[] {ip1, ip2, ip3};
        // 联系人补充
        Contact con1 = TestUtils.createContact();
        con1.LastName = '测试采购科';
        con1.AccountId = acc.Id;
        insert con1;

        Contact con2 = TestUtils.createContact();
        con2.LastName = '测试分管院长';
        con2.Post__c = '分管院长(设备)';
        con2.Departments__c = '医院办公室';
        con2.AccountId = acc.Id;
        insert con2;

        Contact con3 = TestUtils.createContact();
        con3.LastName = '测试财务';
        con3.Post__c = '科长/主任';
        con3.Departments__c = '财务科/计财处';
        con3.AccountId = acc.Id;
        insert con3;

        Contact con4 = TestUtils.createContact();
        con4.LastName = '测试检验';
        con4.Post__c = '科长/主任';
        con4.Departments__c = '检验科';
        con4.AccountId = acc.Id;
        insert con4;
      
        // 科室上年度收入
        Department__c dept = [select Id from Department__c where TerminalCustomer__c = :acc.Id];
        dept.LastYearRevenue__c = 1000;
        update dept;
         System.Test.StartTest();

        Account acc1 = TestUtils.createAccount();
        acc1.name = '迈克实业测试';
        acc1.Province__c = pro.Id;
        acc1.City__c = c.Id;
        acc1.District__c = d.Id;
        acc1.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('Maccura','Account');
        insert acc1;
        
        opp1.StageName = '立项';
        opp1.ProjectType__c = '单项目';
        opp1.Description = 'test1';
        opp1.ProjStartDate__c = Date.today().addDays(-80);
        opp1.ExecuteCompany__c = acc1.Id;
        opp1.ExecutiveCompany__c = acc1.Id;

        update opp1;

        opp2.StageName = '立项';
        opp2.ProjectType__c = '多项目';
        opp2.Description = 'test2';
        opp2.ProjStartDate__c = Date.today().addDays(-80);
        opp2.ExecuteCompany__c = acc1.Id;
        opp2.ExecutiveCompany__c = acc1.Id;
        update opp2;

        System.schedule('CheckOppTimeoutTest', CRON_EXP, new CheckOppTimeout());
        System.Test.StopTest();

        opp1 = [select IsTimeout__c from Opportunity where Id = :opp1.Id];
        System.assertEquals(true, opp1.IsTimeout__c);
    }
}